# -*- coding: utf-8 -*-
#===================================================
# @Time : 2021/3/11 14:21                           
# @Author : weni09                                  
# @File : models.py
#===================================================

from tortoise.models import Model
from tortoise import fields
from tortoise.fields.base import RESTRICT,CASCADE
import setting
from .Fields import EditorField

class Tag(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(20,null=False,unique=True)
    slug = fields.CharField(36,null=False,unique=True,index=True)

    def __str__(self):
        return self.name

    class Meta:
       app = 'blog'

class Category(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(50,null=False,unique=True)
    slug = fields.CharField(36,null=False,unique=True,index=True)
    created_at = fields.DatetimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.pk}#{self.name}"
    class Meta:
       app = 'blog'

class Comment(Model):
    """
        评论
    """
    id = fields.IntField(pk=True)
    article = fields.ForeignKeyField('blog.Article',on_delete=CASCADE,related_name='comments',null=False,description='文章')
    content = EditorField(max_length=500,description="评论内容")
    content_md = EditorField(max_length=500,description="评论内容md格式",default="")
    parent_id = fields.IntField(index=True,default=0,description="父ID")
    root_id = fields.IntField(index=True,default=0,description="根ID")
    nickname = fields.CharField(50,null=False,description="评论者昵称")
    email = fields.CharField(50,null=False,description="评论者邮箱")
    create_date = fields.DatetimeField(auto_now_add=True,description="评论创建时间")

    def __str__(self):
        return f"{self.id}#{self.parent_id}"

    class Meta:
       app = 'blog'

class Article(Model):
    id = fields.IntField(pk=True)
    title = fields.CharField(150,description='文章标题')
    summary = fields.CharField(200, default=getattr(setting, 'DEFAULT_SUMMARY'), description='文章摘要')

    is_public = fields.BooleanField(default=False,description='是否公开')
    is_top = fields.BooleanField(default=False,description='是否置顶')
    body = EditorField(max_length=20000,default='',description='文章内容')
    body_md = EditorField(max_length=20000,default='',description='文章内容md')

    img_link = fields.CharField(50,null=False,default="",description='图片地址')
    create_date = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_date = fields.DatetimeField(auto_now=True,description='更新时间')
    views = fields.IntField(default=0,description='阅览量')
    slug = fields.CharField(36,unique=True)

    author = fields.ForeignKeyField('blog.User',on_delete=RESTRICT,related_name='articles',null=False,description='用户Id')
    category = fields.ForeignKeyField('blog.Category',on_delete=RESTRICT,related_name='articles',null=False,description='文章分类')
    tags = fields.ManyToManyField('blog.Tag',on_delete=CASCADE,related_name='articles',through='article_tag',description='标签')

    def __str__(self):
        return self.title

    class Meta:
       app = 'blog'
       unique_together = (
           ('title', 'author'),
       )


class About(Model):
    id = fields.IntField(pk=True)
    content = EditorField(default="",null=False)
    content_md = EditorField(default="",null=False,)

    def __str__(self):
        return self.id
    class Meta:
       app = 'blog'